package net.schmurgon.gwt.scaffold.generated;

import com.google.gwt.app.client.EditorSupport;
import com.google.gwt.app.client.DoubleBox;
import com.google.gwt.app.client.IntegerBox;
import com.google.gwt.app.client.LongBox;
import com.google.gwt.app.place.RecordEditView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.valuestore.shared.Property;
import com.google.gwt.text.shared.AbstractRenderer;

import net.schmurgon.gwt.request.CourseRecord;
import net.schmurgon.gwt.shared.CourseType;

import java.util.Map;
import java.util.Set;
import java.util.Collection;

/**
 * Edit view for Course records.
 */
public class CourseEditView extends Composite implements RecordEditView<CourseRecord> {
	interface Binder extends UiBinder<HTMLPanel, CourseEditView> {
	}

	interface DataBinder extends EditorSupport<CourseRecord, CourseEditView> {
	}

	private static final Binder BINDER = GWT.create(Binder.class);
	private static final DataBinder DATA_BINDER = GWT.create(DataBinder.class);
	
	@UiField (provided = true) ValueListBox<net.schmurgon.gwt.shared.CourseType> courseType=new ValueListBox<net.schmurgon.gwt.shared.CourseType>(new AbstractRenderer<net.schmurgon.gwt.shared.CourseType>() {
      public String render(net.schmurgon.gwt.shared.CourseType obj) {
        return obj == null ? "" : String.valueOf(obj);
      }    
});
	@UiField DoubleBox distance;
	@UiField TextBox mapUrl;	
	@UiField Button cancel;
	@UiField Button save;
	@UiField InlineLabel id;
	@UiField InlineLabel version;
	@UiField DivElement errors;

	@UiField Element editTitle;
	@UiField Element createTitle;

	private Delegate delegate;
	private CourseRecord record;

	public CourseEditView() {
		initWidget(BINDER.createAndBindUi(this));
		DATA_BINDER.init(this);
	}

	public CourseEditView asWidget() {
		return this;
	}	

	public String[] getPaths() {
		return DATA_BINDER.getPaths();
	}

	public CourseRecord getValue() {
		return record;
	}

	public boolean isChanged() {
		return DATA_BINDER.isChanged(this);
	}

    
    	public void setCourseTypePickerValues(Collection<CourseType> values) {
		courseType.setAcceptableValues(values);
	}


    
    
	public void setCreating(boolean creating) {
		if (creating) {
			editTitle.getStyle().setDisplay(Display.NONE);
			createTitle.getStyle().clearDisplay();
		} else {
			editTitle.getStyle().clearDisplay();
			createTitle.getStyle().setDisplay(Display.NONE);
		}
	}

	public void setDelegate(Delegate delegate) {
		this.delegate = delegate;
	}

	public void setEnabled(boolean enabled) {
		DATA_BINDER.setEnabled(this, enabled);
		save.setEnabled(enabled);
	}

	public void setValue(CourseRecord value) {
		this.record = value;
		DATA_BINDER.setValue(this, value);
	}

	public void showErrors(Map<String, String> errorMap) {
		DATA_BINDER.showErrors(this, errorMap);
	}

	@UiHandler("cancel")
	void onCancel(ClickEvent event) {
		delegate.cancelClicked();
	}

	@UiHandler("save")
	void onSave(ClickEvent event) {
		delegate.saveClicked();
	}
}
